package znbkCNtxl.CNTXL_YDJD;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.WebView;
import android.widget.RelativeLayout;

import com.example.znbk.znbklibrary.base.AnswerViewPagerAdapter;
import com.example.znbk.znbklibrary.base.BaseFragment;
import com.example.znbk.znbklibrary.widget.ScaleButton;
import com.lancoo.znbkxx.R;

import java.util.ArrayList;
import java.util.List;

import znbkCNtxl.GetNtxlJson.CNtxlPaperEntity;

@SuppressLint("ValidFragment")
public class CNtxlYdjdFragment extends BaseFragment {

    private String QuesBody = "";
    private String ChildAsk = "";
    private String SubChildAsk = "";
    private void getEntityInfor(){
        QuesBody = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getQuesBody();
        ChildAsk = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().get(0).getChildAsk();
        SubChildAsk = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();
    }

    private int fromModel = -1;
    private List<CNtxlPaperEntity> CNtxlPaperEntity;
    private int quesIndex = -1;
    private CallBackFragmentYdjd callBackFragmentYdjd;
    public CNtxlYdjdFragment(int fromModel,List<CNtxlPaperEntity> CNtxlPaperEntity, final int quesIndex,CallBackFragmentYdjd callBackFragmentYdjd){
        this.fromModel = fromModel;
        this.CNtxlPaperEntity = CNtxlPaperEntity;
        this.quesIndex = quesIndex;
        this.callBackFragmentYdjd = callBackFragmentYdjd;
        getEntityInfor();
    }

    @Override
    protected int createLayout() {
        return R.layout.cntxl_fragment_ydjd;
    }

    private WebView mWvQuesBody;
    private ViewPager mVpAnswer;
    private ArrayList<View> list;
    public AnswerViewPagerAdapter mAnswerViewPagerAdapter;
//    private Button mBtnSubmit;
    private RelativeLayout mRlControlView;
    private ScaleButton mSBtnSubmit;
    private ScaleButton mSBtnSameQuesTranslate;

    @Override
    protected void initView(View view) {

        //显示Ask题干
        mWvQuesBody = view.findViewById(R.id.wv_quesBody);
        mWvQuesBody.setBackgroundColor(0); // 设置背景色
        mWvQuesBody.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        String WvQuesBody = QuesBody+ChildAsk+SubChildAsk;
        mWvQuesBody.loadData(WvQuesBody+"</br></br>", "text/html", "utf-8");

        //显示Answer信息
        mVpAnswer = view.findViewById(R.id.vp_answerYdjd);
        list = new ArrayList<>();
        list.clear();
        int totalItemCount = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getTotalItemCount();
        for (int i = 0; i < totalItemCount; i++) {
            RecyclerView mRvAnswer = new RecyclerView(getActivity());
            mRvAnswer.setLayoutManager(new LinearLayoutManager(getActivity()));//必须先设置LayoutManager
            CNtxlYdjdAnswerAdapter mAnswerAdapter = new CNtxlYdjdAnswerAdapter(getActivity(),
                    CNtxlPaperEntity,quesIndex,i);
            mRvAnswer.setAdapter(mAnswerAdapter);
            list.add(mRvAnswer);
        }
        mAnswerViewPagerAdapter = new AnswerViewPagerAdapter(list);
        mVpAnswer.setAdapter(mAnswerViewPagerAdapter);
        CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().setCurrentSubChildAnswerIndex(0);
        mVpAnswer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().setCurrentSubChildAnswerIndex(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        mRlControlView = view.findViewById(R.id.rl_controlView);
        mSBtnSubmit = view.findViewById(R.id.sbtn_submit);
        int SubmitState =  CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
        if(SubmitState==2){
            mSBtnSubmit.setVisibility(View.GONE);
        }else{
            mSBtnSubmit.setVisibility(View.VISIBLE);
        }
        mSBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().setSubmitState(2);
                hideSBtnSubmit();
            }
        });

//        mBtnSubmit.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                switch (event.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                        ObjectAnimator scalexShow =  ObjectAnimator.ofFloat(mBtnSubmit,View.SCALE_X,1f,1.1f);
//                        ObjectAnimator scaleYShow =  ObjectAnimator.ofFloat(mBtnSubmit,View.SCALE_Y,1f,1.1f);
//                        AnimatorSet setShow = new AnimatorSet();
//                        setShow.playTogether(scalexShow,scaleYShow);
//                        setShow.setInterpolator(new DecelerateInterpolator());
//                        setShow.setDuration(100);
//                        setShow.start();
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().setSubmitState(2);
//
//                        ObjectAnimator scalexShow1 =  ObjectAnimator.ofFloat(mBtnSubmit,View.SCALE_X,1.1f,1f);
//                        ObjectAnimator scaleYShow1 =  ObjectAnimator.ofFloat(mBtnSubmit,View.SCALE_Y,1.1f,1f);
//                        AnimatorSet setShow1 = new AnimatorSet();
//                        setShow1.playTogether(scalexShow1,scaleYShow1);
//                        setShow1.setInterpolator(new DecelerateInterpolator());
//                        setShow1.setDuration(300);
//                        setShow1.addListener(new Animator.AnimatorListener() {
//                            @Override
//                            public void onAnimationStart(Animator animation) {
//
//                            }
//
//                            @Override
//                            public void onAnimationEnd(Animator animation) {
//                                mBtnSubmit.setVisibility(View.GONE);
//                                callBackFragmentYdjd.submitZgtQuesAnswer();
//                            }
//
//                            @Override
//                            public void onAnimationCancel(Animator animation) {
//
//                            }
//
//                            @Override
//                            public void onAnimationRepeat(Animator animation) {
//
//                            }
//                        });
//                        setShow1.start();
//
//                        break;
//                }
//                return true;
//            }
//        });

        //显示同类题训练按钮
        mSBtnSameQuesTranslate = view.findViewById(R.id.sbtn_sameQuesTranslate);
        mSBtnSameQuesTranslate.setVisibility(View.GONE);

    }

    private void hideSBtnSubmit(){
        ObjectAnimator translationY1 = ObjectAnimator.ofFloat(mRlControlView, View.TRANSLATION_Y, 0f, 300f);
        ObjectAnimator translationY2 = ObjectAnimator.ofFloat(mSBtnSubmit, View.TRANSLATION_Y, 0f, 300f);
        AnimatorSet setShow = new AnimatorSet();
        setShow.playTogether(translationY1,translationY2);
        setShow.setDuration(600);
        setShow.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                callBackFragmentYdjd.submitZgtQuesAnswer();
                mRlControlView.setVisibility(View.GONE);
                mSBtnSubmit.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        setShow.start();
    }


    public interface CallBackFragmentYdjd {
        void submitZgtQuesAnswer();
    }

}